/**
 * Created by netsnower on 16/7/18.
 */

//--------------分享-------------
var baseUrl = location.href.split('?')[0];
baseUrl = baseUrl.indexOf('index.html') == -1 ? (location.href.split('?')[0]+'index.html') : location.href;
var shareLink = location.href;
var shareImg = baseUrl.replace(/index.html.*/,'images/wxShare.jpg');
var shareTxt1 = '广汽本田售后服务体验调查问卷';
var shareTxt2= '广汽本田';
function iniWx() {
    wx.config({
        debug: false,
        appId: signPackage.appId,
        timestamp: parseInt(signPackage.timestamp),
        nonceStr: signPackage.nonceStr,
        signature: signPackage.signature,
        jsApiList: [
            'checkJsApi',
            'onMenuShareTimeline',
            'onMenuShareAppMessage',
            'previewImage'
        ]
    });
    //config接口处理成功后
    wx.ready(function () {
        // 1 判断当前版本是否支持指定 JS 接口，支持批量判断
        wx.checkJsApi({
            jsApiList: [
                'getNetworkType',
                'onMenuShareTimeline',
                'onMenuShareAppMessage',
                'previewImage'
            ],
            success: function (res) {
                //alert(JSON.stringify(res));
                //网络+分享
                checkResult = eval('(' + JSON.stringify(res) + ')');
                checkResult = checkResult.checkResult;
                if (checkResult.getNetworkType) {
                    getNetworkType();
                }
                if (checkResult.onMenuShareTimeline) {
                    onMenuShareTimeline();
                }
                if (checkResult.onMenuShareAppMessage) {
                    onMenuShareAppMessage();
                }
            }
        });
    });
    //错误
    wx.error(function (res) {
        alert(res.errMsg);
    });
}
//------------------------------以下为常规接口-------------------------
//配置检测网络接口
function getNetworkType() {
    wx.getNetworkType({
        success: function (res) {
            console.log(res.networkType);
        },
        fail: function (res) {
            console.log(JSON.stringify(res));
        }
    });
}
function previewImage(url,urls){
    wx.previewImage({
        current: url, // 当前显示图片的http链接
        urls: urls // 需要预览的图片http链接列表
    });
}
//注册获取“发送给朋友”状态事件
function onMenuShareAppMessage() {
    wx.onMenuShareAppMessage({
        title: shareTxt1,
        desc: shareTxt2,
        link: shareLink,
        imgUrl: shareImg,
        trigger: function (res) {
            //alert('用户点击发送给朋友');
        },
        success: function (res) {
            //alert('已分享');
        },
        cancel: function (res) {
            //alert('已取消');
        },
        fail: function (res) {
            //alert(JSON.stringify(res));
        }
    });
}
//注册获取“分享到朋友圈”状态事件
function onMenuShareTimeline() {
    wx.onMenuShareTimeline({
        title: shareTxt1,
        link: shareLink,
        imgUrl: shareImg,
        trigger: function (res) {
            //alert('用户点击分享到朋友圈');
        },
        success: function (res) {
            //alert('已分享');
        },
        cancel: function (res) {
            //alert('已取消');
        },
        fail: function (res) {
            //alert(JSON.stringify(res));
        }
    });
}
